package Engine 
{
	import Engine.utils.Stats;
	import flash.desktop.NativeProcessStartupInfo;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	/**
	 * ...
	 * @author Vijay Ramani
	 */
	
	
	public class Engine extends Sprite 
	{
		private var key_up:Boolean 		= false;
		private var key_down:Boolean 	= false;
		private var key_right:Boolean 	= false;
		private var key_left:Boolean 	= false;
		private var key_space:Boolean 	= false;
		private var key_cntrl:Boolean 	= false;
		private var key_alt:Boolean 	= false;
		private var key_esc:Boolean 	= false;
		
		public function get KeyUp():Boolean
		{
			return key_up;
		}
		public function get KeyDown():Boolean
		{
			return key_down;
		}
		public function get KeyRight():Boolean
		{
			return key_right;
		}
		public function get KeyLeft():Boolean
		{
			return key_left;
		}
		public function get KeySpace():Boolean
		{
			return key_space;
		}
		public function get KeyCntrl():Boolean
		{
			return key_cntrl;
		}
		public function get KeyAlt():Boolean
		{
			return key_alt;
		}
		public function get KeyEsc():Boolean
		{
			return key_esc;
		}
		
		private var mouseDown:Boolean	= false;
		public function get MouseDown():Boolean
		{
			return mouseDown;
		}
		
		
		private var stageWidth:uint 	= 0;
		private var stageHeight:uint 	= 0;
		
		public function get StageWidth():uint
		{
			return stageWidth;
		}
		public function get StageHeight():uint
		{
			return stageHeight;
		}
		
		public function Engine() 
		{
			if (stage) Initialize();
			else addEventListener(Event.ADDED_TO_STAGE, Initialize);
		}
		
		private function Initialize(_event:Event = null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, Initialize);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			stage.addEventListener(Event.RESIZE, Resized);
			
			stageWidth = stage.stageWidth;
			stageHeight = stage.stageHeight;
			
			stage.addEventListener(MouseEvent.MOUSE_DOWN, HandleMouseDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, HandleMouseUp);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, HandleKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, HandleKeyUp);
			this.addEventListener(Event.ENTER_FRAME, Updated);
			
			ResetKeys();
			
			Init();
		}
		
		private function ResetKeys():void
		{
			key_up 		= false;
			key_down 	= false;
			key_right 	= false;
			key_left 	= false;
			key_space 	= false;
			key_cntrl 	= false;
			key_alt 	= false;
			key_esc 	= false;
			
			//trace("ResetKeys: " + key_down);
		}
		
		private function Updated(_event:Event):void
		{
			Update();
		}
		
		private function HandleKeyDown(_event:KeyboardEvent):void
		{
			switch(_event.keyCode)
			{
				case Keyboard.UP:
				key_up = true;
				break;
				case Keyboard.DOWN:
				key_down = true;
				//trace("HandleKeyDown: " + key_down);
				break;
				case Keyboard.RIGHT:
				key_right = true;
				break;
				case Keyboard.LEFT:
				key_left = true;
				break;
				case Keyboard.SPACE:
				key_space = true;
				break;
				case Keyboard.CONTROL:
				key_cntrl = true;
				break;
				case Keyboard.ALTERNATE:
				key_alt = true;
				break;
				case Keyboard.ESCAPE:
				key_esc = true;
				break;
			}
		}
		private function HandleKeyUp(_event:KeyboardEvent):void
		{
			switch(_event.keyCode)
			{
				case Keyboard.UP:
				key_up = false;
				break;
				case Keyboard.DOWN:
				key_down = false;
				//trace("HandleKeyUp: " + key_down);
				break;
				case Keyboard.RIGHT:
				key_right = false;
				break;
				case Keyboard.LEFT:
				key_left = false;
				break;
				case Keyboard.SPACE:
				key_space = false;
				break;
				case Keyboard.CONTROL:
				key_cntrl = false;
				break;
				case Keyboard.ALTERNATE:
				key_alt = false;
				break;
				case Keyboard.ESCAPE:
				key_esc = false;
				break;
			}
		}
		
		private function HandleMouseDown(_event:MouseEvent):void
		{
			mouseDown = true;
		}
		private function HandleMouseUp(_event:MouseEvent):void
		{
			mouseDown = false;
		}
		
		private function Resized(_event:Event):void
		{
			stageWidth = stage.stageWidth;
			stageHeight = stage.stageHeight;
			
			Resize();
		}
		
		public function Init():void
		{
			
		}
		
		public function Update():void
		{
			
		}
		
		public function Resize():void
		{
			
		}
		
		public function get MouseX():uint
		{
			return mouseX;
		}
		public function get MouseY():uint
		{
			return mouseY;
		}
		
	}

}